package cn.iocoder.yudao.module.lfd.service.order;

import cn.iocoder.yudao.module.lfd.controller.admin.appointment.vo.AppointmentEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.lfd.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.lfd.convert.order.OrderConvert;
import cn.iocoder.yudao.module.lfd.dal.mysql.order.OrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.lfd.enums.ErrorCodeConstants.*;

/**
 * 订单 Service 实现类
 *
 * @author 知一
 */
@Service
@Validated
public class OrderService {

    @Resource
    private OrderMapper orderMapper;


    public String createOrder(OrderCreateReqVO createReqVO) {
        // 插入
        OrderDO order = OrderConvert.INSTANCE.convert(createReqVO);
        orderMapper.insert(order);
        order.setId(createReqVO.getId());
        // 返回
        return order.getId();
    }


    
    public void updateOrder(OrderUpdateReqVO updateReqVO) {
        // 校验存在
        validateOrderExists(updateReqVO.getId());
        // 更新
        OrderDO updateObj = OrderConvert.INSTANCE.convert(updateReqVO);
        orderMapper.updateById(updateObj);
    }

    public void updateOrder(OrderDO order) {
        orderMapper.updateById(order);
    }
    
    public void deleteOrder(String id) {
        // 校验存在
        validateOrderExists(id);
        // 删除
        orderMapper.deleteById(id);
    }

    private void validateOrderExists(String id) {
        if (orderMapper.selectById(id) == null) {
            throw exception(ORDER_NOT_EXISTS);
        }
    }

    
    public OrderDO getOrder(String id) {
        return orderMapper.selectById(id);
    }

    
    public List<OrderDO> getOrderList(Collection<String> ids) {
        return orderMapper.selectBatchIds(ids);
    }

    
    public PageResult<OrderDO> getOrderPage(OrderPageReqVO pageReqVO) {
        return orderMapper.selectPage(pageReqVO);
    }

    
    public List<OrderDO> getOrderList(OrderExportReqVO exportReqVO) {
        return orderMapper.selectList(exportReqVO);
    }


    public void createOrder(Long appointmentId, String flowNo) {
        OrderDO orderDO=new OrderDO();
        orderDO.setId(flowNo);
        orderDO.setAppointmentId(appointmentId);
        orderDO.setStatus(AppointmentEnum.PAYING);
        orderMapper.insert(orderDO);
    }
}
